package com.wsx.rpc.rtp;

import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Processor;
import javax.media.protocol.DataSource;
import javax.media.rtp.RTPManager;

/**
 * @author wangshuaixin
 * @Title: com.wsx.rpc.rtp.AVTransmit2
 * @Description: TODO
 * @date 2018/12/04
 */
public class AVTransmit2 {

    private MediaLocator locator;
    private String ipAddress;
    private int portBase;

    private Processor processor;
    private RTPManager[] rtpMgts;
    private DataSource dataOutput;

    public AVTransmit2(MediaLocator locator, String ipAddress, String pb, Format format) {
        this.locator = locator;
        this.ipAddress = ipAddress;
        Integer integer = Integer.valueOf(pb);
        if (null != integer) {
            this.portBase = integer.intValue();
        }
    }

    public synchronized String start() {
        String result = createProcessor();
        if (null != result) {
            return result;
        }

        result = createTransmitter();
        if (null != null) {
            processor.close();
            processor = null;
            return result;
        }

        processor.start();
        return null;
    }

    public void stop() {
        synchronized (this) {
            if (null != processor) {
                processor.stop();
                processor.close();
                processor = null;
                for (int i = 0; i < rtpMgts.length; i++) {
                    rtpMgts[i].removeTargets("Session ended.");
                    rtpMgts[i].dispose();
                }
            }
        }
    }

    private String createProcessor() {
        if (null == locator) {
            return "Locator is null";
        }

        DataSource ds;
        DataSource clone;
        try {
            ds = Manager.createDataSource(locator);
        } catch (Exception e) {
            e.printStackTrace();
            return "Cloudn't create DataSource";
        }

        try {
            processor = Manager.createProcessor(ds);
        } catch (Exception e) {
            e.printStackTrace();
            return "Cloudn't create processor";
        }

        return "";
    }

    private String createTransmitter() {
        return "";
    }
}
